package com.qilu.pe.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qilu.pe.R;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Global;
import com.qilu.pe.dao.bean.Report;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.ui.activity.ReportDetailActivity;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.SizeUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class ReportCheckFragment extends BaseFragment {
    private RecyclerView rcv_list;// 报告列表
    private TextView empty_view;

    private ReportCheckListAdapter adapter;
    private List<Report> reportList = new ArrayList<>();// 报告查看列表数据

    private String status = "";// 区分报告状态 1 未接单,2 运输中,3 检验中,4 已出报告

    public ReportCheckFragment() {

    }

    public static ReportCheckFragment newInstance(String status) {
        ReportCheckFragment fragment = new ReportCheckFragment();
        Bundle args = new Bundle();
        args.putString("status", status);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        status = getArguments().getString("status");
        adapter = new ReportCheckListAdapter(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_report_check, container, false);
        rcv_list = v.findViewById(R.id.rcv_list);
        empty_view = v.findViewById(R.id.empty_view);

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);

        hideProgress();
        reqReportList();
        showProgress();

        return v;
    }

    private Disposable reqReportList() {
        return APIRetrofit.getDefault().reqReportList(Global.HEADER, status)
                .compose(RxUtil.<BaseResult2<List<Report>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<Report>>>() {
                    @Override
                    public void accept(BaseResult2<List<Report>> listBaseResult2) throws Exception {
                        hideProgress();
                        if (listBaseResult2.isSuccess()) {

                        } else {

                        }
                        reportList = listBaseResult2.getData();
                        adapter.setDataList(reportList);
                        if (isDataEmpty(reportList)) {
                            empty_view.setVisibility(View.VISIBLE);
                        } else {
                            empty_view.setVisibility(View.GONE);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                        empty_view.setVisibility(View.VISIBLE);
                    }
                });
    }

    private class ReportCheckListAdapter extends ListBaseAdapter<Report> {

        public ReportCheckListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_report_check_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_order = holder.getView(R.id.tv_order);
            TextView tv_name = holder.getView(R.id.tv_name);
            TextView tv_patient_name = holder.getView(R.id.tv_patient_name);
            TextView tv_unnormal0 = holder.getView(R.id.tv_unnormal0);
            TextView tv_unnormal_num = holder.getView(R.id.tv_unnormal_num);
            TextView tv_test_num = holder.getView(R.id.tv_test_num);
            TextView tv_total_price = holder.getView(R.id.tv_total_price);
            TextView tv_complete_time = holder.getView(R.id.tv_complete_time);
            CardView cv_item = holder.getView(R.id.cv_item);

            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) cv_item.getLayoutParams();
            if (position == 0) {
                params.topMargin = SizeUtils.dp2px(10);
                params.bottomMargin = SizeUtils.dp2px(5);
            } else if (position == mDataList.size() - 1) {
                params.topMargin = SizeUtils.dp2px(5);
                params.bottomMargin = SizeUtils.dp2px(10);
            } else {
                params.topMargin = SizeUtils.dp2px(5);
                params.bottomMargin = SizeUtils.dp2px(5);
            }

            final Report report = mDataList.get(position);
            tv_name.setText(report.getName());
            tv_patient_name.setText(report.getUsername());
            tv_test_num.setText("共" + report.getCount() + "个检测项目");
            tv_total_price.setText("合计" + report.getPrice() + "元");

            if (TextUtils.equals("1", status)) {
                tv_unnormal0.setText("");
                tv_unnormal_num.setText("");
                tv_complete_time.setText("提交日期:" + report.getCreated_at());
                tv_order.setVisibility(View.GONE);
            } else if (TextUtils.equals("2", status)) {
                tv_unnormal0.setText("");
                tv_unnormal_num.setText("");
                tv_complete_time.setText("接单日期:" + report.getCreated_at());
                tv_order.setVisibility(View.GONE);
            } else if (TextUtils.equals("3", status)) {
                tv_unnormal0.setText("");
                tv_unnormal_num.setText("");
                tv_complete_time.setText("开始检验时间:" + report.getCreated_at());
                tv_order.setVisibility(View.VISIBLE);
            } else if (TextUtils.equals("4", status)) {
                tv_unnormal0.setText("异常项:");
                tv_unnormal_num.setText(report.getAbnormal() + "项");
                tv_complete_time.setText("完成时间:" + report.getCreated_at());
                tv_order.setVisibility(View.VISIBLE);
            } else {
                tv_unnormal0.setText("");
                tv_unnormal_num.setText("");
                tv_complete_time.setText("");
                tv_order.setVisibility(View.GONE);
            }

            // 1待付款,2申请退款,3订单取消,4已退款,5未接单,6运输中,7检验中,8已出报告
            String type = report.getType();

            tv_order.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle = new Bundle();
                    bundle.putString("order_id", report.getOrder_id());
                    startActivity(ReportDetailActivity.class, bundle);
                }
            });
        }
    }
}